package gptma.fo.controler.project;

import gptma.bo.model.project.Project;
import gptma.bo.model.request.ClientRequest;
import gptma.bo.model.request.RequestType;
import gptma.bo.services.project.ProjectServices;
import gptma.fo.cdi.ViewScoped;
import gptma.fo.controler.WebPage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;

/**
 * 
 * @author xcl
 */
@Named
@ViewScoped
public class ProjectReportingPage extends WebPage implements Serializable {

	private static final long serialVersionUID = 859291632092526686L;

	@EJB
	private ProjectServices projectServices;

	private List<ProjectBean> projectBeans;

	public void initReportingPage() {
		projectBeans = new ArrayList<ProjectBean>();

		List<Project> projects = projectServices.findProjectsByName("Safran%");
		int evolutionCount;
		int defectCount;
		RequestType type;
		List<ClientRequest> projectRequests;
		for (Project project : projects) {
			evolutionCount = 0;
			defectCount = 0;
			projectRequests = projectServices.findRequests(project);
			for (ClientRequest clientRequest : projectRequests) {
				type = clientRequest.getRequestType();
				if (type == RequestType.DE || type == RequestType.OT) {
					evolutionCount++;
				} else if (type == RequestType.RA) {
					defectCount++;
				}
			}

			projectBeans.add(new ProjectBean(project, evolutionCount,
					defectCount));
		}
	}

	public List<ProjectBean> getProjectBeans() {
		return projectBeans;
	}

}
